package com.example.demo.threadpool;

import java.util.concurrent.*;

/**
 * ClassName: ThreadPoolApiDemo
 * author: bob.ly
 * Version: 1.0.0
 * DateTime: 2021/08/01-11:11:00
 * Description:
 */
public class ThreadPoolApiDemo {
    public static void main(String[] args) {
        /**
         * 火车站3个售票口(全是核心线程), 5000个用户买票
         *
         * 类似newFixedThreadPool常见线程池, 最大线程池=核心线程池。
         */
        ExecutorService executorService = new ThreadPoolExecutor(
                3,
                3,
                60L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );

        /**
         * 我们自己就无需创建线程了, 线程池以及帮我们创建好了
         */
        for (int i = 1; i <= 5000; i++) {
            executorService.execute(() -> {
                System.out.println(Thread.currentThread().getName() + "号窗口,开始售票");
                System.out.println(Thread.currentThread().getName() + "号窗口,卖票结束");
            });
        }
        /**
         * 完成后要关闭线程。不接收新的任务了, 只将手上和队列的任务处理完
         */
        executorService.shutdown();
    }
}
